//
//  SafeCategoryViewController.m
//  CategoryKit
//
//  Created by xinpenghui on 16/12/20.
//  Copyright © 2016年 xinpenghui. All rights reserved.
//

#import "SafeCategoryViewController.h"
#import "AppUIDefine.h"
#import "SafeCategory.h"

@interface SafeCategoryViewController ()
@property (nonatomic, strong) UITextView *tfView;
@property (nonatomic, strong) NSString *resString;
@end

@implementation SafeCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"SafeCategory";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tfView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, kDefaultWindowWidth-40, 420)];
    _tfView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:_tfView];
    self.resString = @"";
    
    [self arraySafeTestCase];
    
    [self dictionarySafeTestCase];
    
    
}

/**
 NSArray的Category的方法
 对NSArray进行safe方法
 */
- (void)arraySafeTestCase {
    
    NSArray *testArray = @[@"123",@"345",@"543"];
    self.resString = [self.resString stringByAppendingFormat:@"NSArray Category kit safeObjectAtIndex Test Case = %@\n",[testArray safeObjectAtIndex:20]];
    // erro test
    NSArray *testArray1 = [NSArray safeArrayFromObject:testArray];
    self.resString = [self.resString stringByAppendingFormat:@"NSArray Category kit safeArrayFromObject Test Case = %@\n",testArray1];
    
    NSMutableArray *mutArray = [[NSMutableArray alloc] initWithArray:testArray];
    NSMutableArray *reverArray = [mutArray reverse];
    self.resString = [self.resString stringByAppendingFormat:@"NSMutableArray Category kit safeArrayFromObject Test Case = %@\n",reverArray];
    [mutArray safeAddObject:@"2"]; // 不可为数组
    self.resString = [self.resString stringByAppendingFormat:@"NSMutableArray Category kit safeAddObject Test Case = %@\n",mutArray];
 
    self.tfView.text = self.resString;
}

/**
 NSDictionary的Category的方法
 对NSDictionary进行safe方法
 */
- (void)dictionarySafeTestCase {
    
    NSDictionary *testDic = @{@"num1":@"123",@"num2":@"345",@"num3":@"543"};
//    [testDic safeSetObject:@"hhh" forKey:@"num1"];
//    self.resString = [self.resString stringByAppendingFormat:@"NSDictionary Category kit safeSetObject Test Case = %@\n",testDic];
//    [testDic safeSetObject:@"3" forKey:@"num1"];
//    self.resString = [self.resString stringByAppendingFormat:@"NSDictionary Category kit safeSetObject nil Test Case = %@\n",testDic];
    
    NSDictionary *testDic2 = [NSDictionary safeDictionaryFromObject:testDic];
    self.resString = [self.resString stringByAppendingFormat:@"NSDictionary Category kit safeDictionaryFromObject Test Case = %@\n",testDic2];
    
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] initWithDictionary:testDic];
    [mutDic safeSetObject:@"23334444" forKey:@"num2"];
    self.resString = [self.resString stringByAppendingFormat:@"NSMutableDictionary Category kit safeSetObject Test Case = %@\n",mutDic];

    // 先取object 无值时取default
    [mutDic setObject:nil defaultObject:@"hhhl" forKey:@"num1"];
    self.resString = [self.resString stringByAppendingFormat:@"NSMutableDictionary Category kit setObject defaultObject Test Case = %@\n",mutDic];
    
    [mutDic safeSetObject:@"1qqq" defaultValue:@"ddd" forKey:@"num3"];
    self.resString = [self.resString stringByAppendingFormat:@"NSMutableDictionary Category kit safeSetObject defaultObject Test Case = %@\n",mutDic];
    
    self.tfView.text = self.resString;
}
///**
// NSDictionary的Category的方法
// 对NSDictionary进行safe方法
// */
//- (void)dictionarySafeTestCase {
//}

/**
 NSManagedObject的Category的方法
 对NSManagedObject进行safe方法  coreData相关 FFSmart仓使用
 */
- (void)NSManagedObjectSafeTestCase {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
